<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        html,body{
            width: 100%;
            height: 100%;
        }
        .box{
            width: 1000px;
            margin: 0 auto;
        }
        .item{
            margin-top: 10px;
            height: 150px;
            width: 1000px;
            display: flex;
            justify-content: space-around;
            align-items: center;
            border: 1px solid #ccc;
        }
    </style>
    <script src="./lib/axios.js"></script>
</head>
<body>
    <a href="./page/sj.html">我的收藏</a>
    <div class="box"></div>
    <script>
        let box=document.querySelector('.box')

        axios.get('/a').then((res) => {
            console.log(res.data);
            fn(res.data)
        })

        function fn(arr){
            box.innerHTML=arr.map((item)=>{
                return `
            <div class="item">
            <img src="${item.img}" alt="">
            <div>
                <p>${item.name}</p>
                <p>${item.author}</p>
                <p>${item.info}</p>
                <p>${item.lTime}</p>
            </div>
            <div>
                <p>${item.count}票</p>
                <button>书籍详情</button>
                <button>加入书架</button>
            </div>
        </div>
                `
            }).join('')
        }

        // 给box添加点击事件
        box.onclick = function(e) {
            let el=e.target;
            if(el.innerHTML==='书籍详情'){
                // 获取下标
                let index=[...box.children].indexOf(el.parentNode.parentNode)
                localStorage.setItem('index',index);
                location.href='./page/xq.html'
            }
            if(el.innerHTML==='加入书架'){
                el.innerHTML=   '已加入书架';
                el.disabled=true;

                // 获取下标
                let index=[...box.children].indexOf(el.parentNode.parentNode)
                axios.get(`/likeBook/index/${index}`).then((res) => {
                    console.log(res.data);
                })
            }
        }
    </script>
</body>
</html>